package util;

import java.io.*;
import java.net.*;



public class HttpUtil {
    /**
     * post 请求
     *
     * @param url
     * @param params key1=value1&key2=value2
     * @return
     * @throws IOException
     */
    public static InputStream sendPost(String url, String params, String sign) throws SocketTimeoutException {
        URL realurl = null;
        InputStream in = null;
        HttpURLConnection conn = null;
        try {

            realurl = new URL(url);
            conn = (HttpURLConnection) realurl.openConnection();
            conn.setConnectTimeout(8000);// 8秒
            conn.setReadTimeout(8000);// 8秒
            conn.setDoOutput(true);
            conn.setUseCaches(false);//不能使用缓存
            conn.setRequestMethod("POST");
            //conn.setRequestProperty("extend", sign);
            PrintWriter pw = new PrintWriter(conn.getOutputStream());
            //pw.print("p="+ URLEncoder.encode(params,"UTF-8"));
            pw.print(params);
            pw.flush();
            pw.close();
            in = conn.getInputStream();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            if(e instanceof SocketTimeoutException){
                SocketTimeoutException e1 = (SocketTimeoutException)e;
                throw e1;
            }
            e.printStackTrace();
        }
        return in;
    }

    public static InputStream sendPost(String url, String params )     {
        URL realurl = null;
        InputStream in = null;
        HttpURLConnection conn = null;
        try {

            realurl = new URL(url);
            conn = (HttpURLConnection) realurl.openConnection();
            conn.setConnectTimeout(8000);// 8秒
            conn.setReadTimeout(8000);// 8秒
            conn.setDoOutput(true);
            conn.setUseCaches(false);//不能使用缓存
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type","application/json;charset=UTF-8");
            PrintWriter pw = new PrintWriter(conn.getOutputStream());
            pw.print(params);
            pw.flush();
            pw.close();
            in = conn.getInputStream();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            if(e instanceof SocketTimeoutException){
                SocketTimeoutException e1 = (SocketTimeoutException)e;
                //throw e1;
            }
            e.printStackTrace();
        }
        return in;
    }

    /**
     * @param url http://xx.sss.com?a=b&aa=bb
     * @return
     * @throws IOException
     */
    public static InputStream sendGet(String url, String sign) {
        URL realurl = null;
        InputStream in = null;
        HttpURLConnection conn = null;
        try {
            realurl = new URL(url);
            conn = (HttpURLConnection) realurl.openConnection();
            conn.setConnectTimeout(8000);// 8秒
            conn.setReadTimeout(8000);// 8秒
            conn.setUseCaches(false);//不能使用缓存
            conn.setRequestMethod("GET");
            conn.setRequestProperty("extend", sign);
            conn.setRequestProperty("Content-Type","application/json;charset=UTF-8");

            in = conn.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return in;
    }


    public static byte[] streamToBytes(InputStream in) {
        if(in==null){
            return null;
        }
        byte[] bytes = null;
        BufferedInputStream bin = new BufferedInputStream(in);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BufferedOutputStream bos = new BufferedOutputStream(baos);
        byte[] buffer = new byte[1028 * 8];
        int length = 0;
        try {
            while ((length = bin.read(buffer)) > 0) {
                bos.write(buffer, 0, length);
            }
            bos.flush();
            bytes = baos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                bin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return bytes;
        }
    }



    public static String stream2String(InputStream in) {
        if (in == null) {
            return null;
        }
        InputStreamReader bin = new InputStreamReader(in);
        BufferedReader br = new BufferedReader(bin);
        StringBuilder sb = new StringBuilder();
        String line = null;
        try {
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return sb.toString();
        }
    }
}
